package jmine.tec.report.metadata;

import java.util.Calendar;
import java.util.Date;

import jmine.tec.utils.date.DatePeriod;
import jmine.tec.utils.date.PastDatePeriod;

/**
 * Defines a parameter type for a column.
 * 
 * @author takeshi
 */
public enum ParameterType {
    NUMBER("maps.report.column.parameter.type.number", Number.class), DATE("maps.report.column.parameter.type.date", Date.class,
            Calendar.class), STRING("maps.report.column.parameter.type.string", String.class), OBJECT(
            "maps.report.column.parameter.type.object", Object.class), ENTITY("maps.report.column.parameter.type.entity", Long.class,
            Long.TYPE), PERCENTAGE("maps.report.column.parameter.type.percentage", Double.class, Double.TYPE), BOOLEAN(
            "maps.report.column.parameter.type.boolean", Boolean.class, Boolean.TYPE), INTEGER("maps.report.column.parameter.type.integer",
            Integer.class, Integer.TYPE), DATE_PERIOD("maps.report.column.parameter.type.datePeriod", DatePeriod.class, Date.class,
            Calendar.class, PastDatePeriod.class), ENUM("maps.report.column.parameter.type.enum", Enum.class), CUSTOM("", Object.class);

    private final Class<?>[] allowedTypes;

    private final String bundleKeyName;

    /**
     * C'tor
     * 
     * @param key the key
     * @param allowedTypes the allowed types for this parameter
     */
    private ParameterType(final String key, final Class<?>... allowedTypes) {
        this.allowedTypes = allowedTypes;
        this.bundleKeyName = key;
    }

    /**
     * Tests wheter or not the given type is allowed for this particular parameter.
     * 
     * @param type the type to check;
     * @return boolean
     */
    public boolean allowedType(final Class<?> type) {
        for (Class<?> allowed : this.allowedTypes) {
            if (allowed.isAssignableFrom(type)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Para ser usado como chave em arquivo de bundle.
     * 
     * @return the bundleKeyName
     */
    public String getBundleKeyName() {
        return this.bundleKeyName;
    }
}
